package cn.ymz.observe;


import javafx.scene.control.TextArea;

import java.util.*;

/**
 * 观察者
 */
public class Observe implements Observer {

    private final static Observe INSTANCE = new Observe();

    public static Observe getInstance() {
        return INSTANCE;
    }

    private TextArea textArea;

    private Observe() {
    }

    public void setTextArea(TextArea textArea) {
        this.textArea = textArea;
    }

    @Override
    public void update(Observable o, Object arg) {
        if (this.textArea == null) {
            return;
        }
        Subject subject = (Subject) o;
        List<String> logs = subject.getLogs();
        Iterator<String> iterator = logs.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            this.textArea.appendText(next);
            this.textArea.appendText("\n\n");
            iterator.remove();
        }
    }

}
